<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Map class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-deque.unshift.html">« Ds\Deque::unshift</a></li>
      <li style="float: right;"><a href="ds-map.allocate.html">Ds\Map::allocate »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.ds.html">Data Structures</a></li>
    <li>The Map class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.ds-map" class="reference">

 <h1 class="title">The Map class</h1>
 

 <div class="partintro"><p class="verinfo">(No version information available, might only be in Git)</p>


  <div class="section" id="ds-map.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    A Map is a sequential collection of key-value pairs, almost identical to an
    <span class="type">array</span> used in a similar context. Keys can be any type, but must be unique.
    Values are replaced if added to the map using the same key.
   </p>
  </div>



<div class="section" id="ds-map.strengths">
    <h2 class="title">Strengths</h2>
    <p class="para">
        <ul class="simplelist">
            <li class="member">Keys and values can be any type, including objects.</li>
            <li class="member">Supports array syntax (square brackets).</li>
            <li class="member">Insertion order is preserved.</li>
            <li class="member">Performance and memory efficiency is very similar to an <span class="type">array</span>.</li>
            <li class="member">Automatically frees allocated memory when its size drops low enough.</li>
        </ul>
   </p>
</div>

<div class="section" id="ds-map.weaknesses">
    <h2 class="title">Weaknesses</h2>
    <p class="para">
        <ul class="simplelist">
            <li class="member">Can’t be converted to an array when objects are used as keys.</li>
        </ul>
   </p>
</div>


  <div class="section" id="ds-map.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Ds\Map</strong>
     </span>

     <span class="oointerface"><span class="modifier">implements</span> 
       <strong class="interfacename">Ds\Collection</strong></span><span class="oointerface">,  <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Constants */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.ds-map.html#ds-map.constants.min-capacity"><var class="varname">MIN_CAPACITY</var></a></var><span class="initializer"> = 16</span>;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="ds-map.allocate.html" class="methodname">allocate</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$capacity</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.apply.html" class="methodname">apply</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.capacity.html" class="methodname">capacity</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.clear.html" class="methodname">clear</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.copy.html" class="methodname">copy</a></span>(): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.diff.html" class="methodname">diff</a></span>(<span class="methodparam"><span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span> <code class="parameter">$map</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.filter.html" class="methodname">filter</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.first.html" class="methodname">first</a></span>(): <span class="type"><a href="class.ds-pair.html" class="type Ds\Pair">Ds\Pair</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.get.html" class="methodname">get</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$default</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.haskey.html" class="methodname">hasKey</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.hasvalue.html" class="methodname">hasValue</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.intersect.html" class="methodname">intersect</a></span>(<span class="methodparam"><span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span> <code class="parameter">$map</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.isempty.html" class="methodname">isEmpty</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.keys.html" class="methodname">keys</a></span>(): <span class="type"><a href="class.ds-set.html" class="type Ds\Set">Ds\Set</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.ksort.html" class="methodname">ksort</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.ksorted.html" class="methodname">ksorted</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.last.html" class="methodname">last</a></span>(): <span class="type"><a href="class.ds-pair.html" class="type Ds\Pair">Ds\Pair</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.map.html" class="methodname">map</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.merge.html" class="methodname">merge</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$values</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.pairs.html" class="methodname">pairs</a></span>(): <span class="type"><a href="class.ds-sequence.html" class="type Ds\Sequence">Ds\Sequence</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.put.html" class="methodname">put</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.putall.html" class="methodname">putAll</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pairs</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.reduce.html" class="methodname">reduce</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$initial</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.remove.html" class="methodname">remove</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$default</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.reverse.html" class="methodname">reverse</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.reversed.html" class="methodname">reversed</a></span>(): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.skip.html" class="methodname">skip</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$position</code></span>): <span class="type"><a href="class.ds-pair.html" class="type Ds\Pair">Ds\Pair</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.slice.html" class="methodname">slice</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.sort.html" class="methodname">sort</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.sorted.html" class="methodname">sorted</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.sum.html" class="methodname">sum</a></span>(): <span class="type"><span class="type">int</span>|<span class="type">float</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.toarray.html" class="methodname">toArray</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.union.html" class="methodname">union</a></span>(<span class="methodparam"><span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span> <code class="parameter">$map</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.values.html" class="methodname">values</a></span>(): <span class="type"><a href="class.ds-sequence.html" class="type Ds\Sequence">Ds\Sequence</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-map.xor.html" class="methodname">xor</a></span>(<span class="methodparam"><span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span> <code class="parameter">$map</code></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>

   }</div>


  </div>


  <div class="section" id="ds-map.constants">
   <h2 class="title">预定义常量</h2>
   <dl>

    
     <dt id="ds-map.constants.min-capacity"><strong><code>Ds\Map::MIN_CAPACITY</code></strong></dt>

     <dd>

      <p class="para"/>
     </dd>

    

   </dl>

  </div>


  <div class="section">
   <h2 class="title">更新日志</h2>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>版本</th>
        <th>说明</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>PECL ds 1.3.0</td>
        <td>
         The class now implements <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span>.
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </div>

 </div>

 











































































































































































































































































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="ds-map.allocate.html">Ds\Map::allocate</a> — Allocates enough memory for a required capacity</li><li><a href="ds-map.apply.html">Ds\Map::apply</a> — Updates all values by applying a callback function to each value</li><li><a href="ds-map.capacity.html">Ds\Map::capacity</a> — Returns the current capacity</li><li><a href="ds-map.clear.html">Ds\Map::clear</a> — Removes all values</li><li><a href="ds-map.construct.html">Ds\Map::__construct</a> — Creates a new instance</li><li><a href="ds-map.copy.html">Ds\Map::copy</a> — Returns a shallow copy of the map</li><li><a href="ds-map.count.html">Ds\Map::count</a> — Returns the number of values in the map</li><li><a href="ds-map.diff.html">Ds\Map::diff</a> — Creates a new map using keys that aren't in another map</li><li><a href="ds-map.filter.html">Ds\Map::filter</a> — Creates a new map using a callable to determine which pairs to include</li><li><a href="ds-map.first.html">Ds\Map::first</a> — Returns the first pair in the map</li><li><a href="ds-map.get.html">Ds\Map::get</a> — Returns the value for a given key</li><li><a href="ds-map.haskey.html">Ds\Map::hasKey</a> — Determines whether the map contains a given key</li><li><a href="ds-map.hasvalue.html">Ds\Map::hasValue</a> — Determines whether the map contains a given value</li><li><a href="ds-map.intersect.html">Ds\Map::intersect</a> — Creates a new map by intersecting keys with another map</li><li><a href="ds-map.isempty.html">Ds\Map::isEmpty</a> — Returns whether the map is empty</li><li><a href="ds-map.jsonserialize.html">Ds\Map::jsonSerialize</a> — Returns a representation that can be converted to JSON</li><li><a href="ds-map.keys.html">Ds\Map::keys</a> — Returns a set of the map's keys</li><li><a href="ds-map.ksort.html">Ds\Map::ksort</a> — Sorts the map in-place by key</li><li><a href="ds-map.ksorted.html">Ds\Map::ksorted</a> — Returns a copy, sorted by key</li><li><a href="ds-map.last.html">Ds\Map::last</a> — Returns the last pair of the map</li><li><a href="ds-map.map.html">Ds\Map::map</a> — Returns the result of applying a callback to each value</li><li><a href="ds-map.merge.html">Ds\Map::merge</a> — Returns the result of adding all given associations</li><li><a href="ds-map.pairs.html">Ds\Map::pairs</a> — Returns a sequence containing all the pairs of the map</li><li><a href="ds-map.put.html">Ds\Map::put</a> — Associates a key with a value</li><li><a href="ds-map.putall.html">Ds\Map::putAll</a> — Associates all key-value pairs of a traversable object or array</li><li><a href="ds-map.reduce.html">Ds\Map::reduce</a> — Reduces the map to a single value using a callback function</li><li><a href="ds-map.remove.html">Ds\Map::remove</a> — Removes and returns a value by key</li><li><a href="ds-map.reverse.html">Ds\Map::reverse</a> — Reverses the map in-place</li><li><a href="ds-map.reversed.html">Ds\Map::reversed</a> — Returns a reversed copy</li><li><a href="ds-map.skip.html">Ds\Map::skip</a> — Returns the pair at a given positional index</li><li><a href="ds-map.slice.html">Ds\Map::slice</a> — Returns a subset of the map defined by a starting index and length</li><li><a href="ds-map.sort.html">Ds\Map::sort</a> — Sorts the map in-place by value</li><li><a href="ds-map.sorted.html">Ds\Map::sorted</a> — Returns a copy, sorted by value</li><li><a href="ds-map.sum.html">Ds\Map::sum</a> — Returns the sum of all values in the map</li><li><a href="ds-map.toarray.html">Ds\Map::toArray</a> — Converts the map to an array</li><li><a href="ds-map.union.html">Ds\Map::union</a> — Creates a new map using values from the current instance and another map</li><li><a href="ds-map.values.html">Ds\Map::values</a> — Returns a sequence of the map's values</li><li><a href="ds-map.xor.html">Ds\Map::xor</a> — Creates a new map using keys of either the current instance or of another map, but not of both</li></ul>
</div>
</div></div></body></html>